package com.ruoyi.module.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.page.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.module.app.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.module.app.service.ISysUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户信息Controller
 *
 * @author mars
 * @date 2023-03-10
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/app/user" )
public class SysUserController extends BaseController {

    private final ISysUserService iSysUserService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('app:user:list')")
    @GetMapping("/list")
    public PageInfo list(SysUser sysUser) {
        return iSysUserService.findPageBySpec(sysUser);
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:user:query')" )
    @GetMapping(value = "/query/{userId}" )
    public Result getInfo(@PathVariable("userId" ) Long userId) {
        return Result.success(iSysUserService.getById(userId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('app:user:add')" )
    @Log(title = "用户信息" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody SysUser sysUser) {
        return Result.success("新增成功",iSysUserService.add(sysUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('app:user:edit')" )
    @Log(title = "用户信息" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser) {
        return toAjax(iSysUserService.update(sysUser) ? 1 : 0);
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('app:user:remove')" )
    @Log(title = "用户信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{userIds}" )
    public Result remove(@PathVariable Long[] userIds) {
        return toAjax(iSysUserService.deleteBatch(Arrays.asList(userIds)) ? 1 : 0);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('app:user:export')" )
    @Log(title = "用户信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public Result export(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<SysUser>(sysUser);
        List<SysUser> list = iSysUserService.list(lqw);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser. class);
        return util.exportExcel(list, "user" );
    }
}
